package com.ntschy.common.valid.dateRange;


import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil;

import java.time.LocalDate;
import java.util.regex.Pattern;

public class DateRangeValidationUtil {

    private static final Pattern PATTERN = Pattern.compile("^((?!0000)[0-9]{4}-((0[1-9]|1[0-2])-(0[1-9]|1[0-9]|2[0-8])|(0[13-9]|1[0-2])-(29|30)|(0[13578]|1[02])-31)|([0-9]{2}(0[48]|[2468][048]|[13579][26])|(0[48]|[2468][048]|[13579][26])00)-02-29)/((?!0000)[0-9]{4}-((0[1-9]|1[0-2])-(0[1-9]|1[0-9]|2[0-8])|(0[13-9]|1[0-2])-(29|30)|(0[13578]|1[02])-31)|([0-9]{2}(0[48]|[2468][048]|[13579][26])|(0[48]|[2468][048]|[13579][26])00)-02-29)$");
    /**
     * 验证日期格式 2020-01-01/2020-11-11
     */
    public static boolean isDateRange(String dateRange){
        if (StrUtil.isEmpty(dateRange)) {
            return false;
        } else {
            boolean matches = PATTERN.matcher(dateRange).matches();
            if(matches) {
                String[] split = dateRange.split("/");
                LocalDate start = LocalDateTimeUtil.parseDate(split[0], DatePattern.NORM_DATE_PATTERN);
                LocalDate end = LocalDateTimeUtil.parseDate(split[1], DatePattern.NORM_DATE_PATTERN);
                return start.isBefore(end);
            } else {
                return false;
            }
        }
    }

}
